package io.renren.wh.channel.controller;

import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.wh.channel.entity.ChannelInfoEntity;
import io.renren.wh.channel.service.ChannelInfoService;
import io.renren.wh.channel.vo.ChannelInfoSaveVo;
import io.renren.wh.interfaces.service.InterfacesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


@Api(value = "通道信息接口",tags = "通道信息接口")
@RestController
@RequestMapping("wh/channelinfo")
public class ChannelInfoController {

    @Autowired
    private ChannelInfoService channelInfoService;

    @Autowired
    private InterfacesService interfacesService;

    /**
     * 列表
     */
    @ApiOperation(value = "获取通道信息列表", notes = "获取通道信息列表", httpMethod = "GET")
    @GetMapping("/list")
    @RequiresPermissions("wh:channel:list")
    public R list(
            @ApiParam(name = "page", value = "第几页", required = false)
            @RequestParam(required = false) String page,
            @ApiParam(name = "limit", value = "每页显示的条数", required = false)
            @RequestParam(required = false) String limit,
            @ApiParam(name = "channelname", value = "通道名称", required = false)
            @RequestParam(required = false)String channelname,
            @ApiParam(name = "interfacename", value = "接口名称", required = false,hidden = true)
            @RequestParam(required = false)String interfacename,
            @ApiParam(name = "interfaceId", value = "接口ID", required = false)
            @RequestParam(required = false)String interfaceId,
            @ApiParam(name = "status", value = "通道状态 0-正常 1-停用", required = false)
            @RequestParam(required = false)String status
            ){

        Map<String,Object> params = new HashMap<>();
        params.put("channelname",channelname);
        params.put("interfacename",interfacename);
        params.put("status",status);
        params.put("interfaceId",interfaceId);
        params.put(Constant.PAGE,page);
        params.put(Constant.LIMIT,limit);

        PageUtils pages = channelInfoService.queryPage(params);
        return R.ok().put("page", pages);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "获取通道信息", notes = "获取通道信息", httpMethod = "GET")
    @GetMapping("/info/{id}")
    @RequiresPermissions("wh:channel:info")
    public R info(@ApiParam(name = "id", value = "通道信息ID") @PathVariable("id") Integer id){

		ChannelInfoEntity channelInfo = channelInfoService.getById(id);

        return R.ok().put("channelInfo", channelInfo);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增通道信息", notes = "新增通道信息", httpMethod = "POST")
    @PostMapping("/save")
    @RequiresPermissions("wh:channel:save")
    public R save(@RequestBody ChannelInfoSaveVo vo){
        channelInfoService.insert(vo);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改通道信息", notes = "修改通道信息", httpMethod = "POST")
    @RequiresPermissions("wh:channel:update")
    public R update(@RequestBody ChannelInfoSaveVo vo){
        channelInfoService.modify(vo);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除通道信息", notes = "删除通道信息", httpMethod = "DELETE")
    @DeleteMapping("/delete")
    @RequiresPermissions("wh:channel:delete")
    public R delete(@ApiParam(name = "ids", value = "通道信息ID") @RequestBody Integer[] ids){
        channelInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
